"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServerManager = void 0;
const HallServer_SocketMsgHandler_1 = require("../../hall_server/HallServer_SocketMsgHandler");
const RoomModel_1 = require("../../hall_server/model/RoomModel");
const RoomTemplateModel_1 = require("../../hall_server/model/RoomTemplateModel");
const ServerConfig_1 = require("../config/ServerConfig");
const SocketClientReqManager_1 = require("../engine/socketclient/SocketClientReqManager");
const SocketConnectionManager_1 = require("../engine/socketserver/socket/SocketConnectionManager");
const Logger_1 = require("../engine/utils/Logger");
const GameHttpHelper_1 = require("../game/helper/GameHttpHelper");
class ServerManager {
    // public initServerSockets() {
    //     let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
    //     for (let i = 0; i < hall_server_list.length; i++) {
    //         let server: ServerConfig = hall_server_list[i]
    //         if (server.id != ServerConfig.nowServerConfig.id) {
    //             SocketClientReqManager.instance.req_server_login(server)
    //         }
    //     }
    // }
    kill_all_users() {
        return __awaiter(this, void 0, void 0, function* () {
            let all_server_list = ServerConfig_1.ServerConfig.getAllSocketServer();
            for (let i = 0; i < all_server_list.length; i++) {
                let server = all_server_list[i];
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_kill_all_users(server);
            }
        });
    }
    set_allserver_status(status) {
        return __awaiter(this, void 0, void 0, function* () {
            let all_server_list = ServerConfig_1.ServerConfig.getAllSocketServer();
            for (let i = 0; i < all_server_list.length; i++) {
                let server = all_server_list[i];
                server.status = status;
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_set_server_status(status, server);
                }
            }
        });
    }
    req_server_update_online(userids, server) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server = ServerConfig_1.ServerConfig.randomHallServer();
            SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_update_online(userids, server.id, server.serverType, hall_server);
        });
    }
    getServerListOnlineNum(serverList) {
        return __awaiter(this, void 0, void 0, function* () {
            let onlineNum = 0;
            Logger_1.Logger.log("get_all_server_online_num=serverList=", serverList);
            for (let i = 0; i < serverList.length; i++) {
                let server = serverList[i];
                // let count:number = await ShellHelper.getOnline(server.socket_port)
                // onlineNum += count
                let response = yield GameHttpHelper_1.GameHttpHelper.get_online_num(server.getInternalUrl() + "/online");
                if (response["count"]) {
                    onlineNum += response["count"];
                }
            }
            return onlineNum;
        });
    }
    get_all_server_online_num() {
        return __awaiter(this, void 0, void 0, function* () {
            let onlineNum = 0;
            let server_list = ServerConfig_1.ServerConfig.hall_server_List;
            // onlineNum += await this.getServerListOnlineNum(server_list)
            // server_list = ServerConfig.huanghua_mj_server_list
            // onlineNum += await this.getServerListOnlineNum(server_list)
            // server_list = ServerConfig.ddz_server_list
            // onlineNum += await this.getServerListOnlineNum(server_list)
            // server_list = ServerConfig.jianziding_server_list
            // onlineNum += await this.getServerListOnlineNum(server_list)
            // server_list = ServerConfig.cangxian_mj_server_list
            // onlineNum += await this.getServerListOnlineNum(server_list)
            // server_list = ServerConfig.paodekuai_server_list
            // onlineNum += await this.getServerListOnlineNum(server_list)
            // server_list = ServerConfig.dazhonger_mj_server_list
            // onlineNum += await this.getServerListOnlineNum(server_list)
            return onlineNum;
        });
    }
    get_all_hallserver_online_num() {
        return __awaiter(this, void 0, void 0, function* () {
            let onlineNum = 0;
            let server_list = ServerConfig_1.ServerConfig.hall_server_List;
            onlineNum += yield this.getServerListOnlineNum(server_list);
            return onlineNum;
        });
    }
    is_in_server(account) {
        return __awaiter(this, void 0, void 0, function* () {
            let resultServerConfig = null;
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                let isIn = yield SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_is_in_server(account, server);
                if (isIn) {
                    resultServerConfig = server;
                    return resultServerConfig;
                }
            }
            return resultServerConfig;
        });
    }
    is_user_online(userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let isOnline = false;
            let all_server_list = ServerConfig_1.ServerConfig.getAllSocketServer();
            for (let i = 0; i < all_server_list.length; i++) {
                let server = all_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    isOnline = SocketConnectionManager_1.SocketConnectionManager.instance.isUserOnline(userid);
                    if (isOnline) {
                        break;
                    }
                }
                else {
                    isOnline = yield SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_is_user_online(userid, server);
                    if (isOnline) {
                        break;
                    }
                }
            }
            return isOnline;
        });
    }
    getServerListClubOnlineNum(server_list, clubid) {
        return __awaiter(this, void 0, void 0, function* () {
            let onlineNum = 0;
            let count = 0;
            for (let i = 0; i < server_list.length; i++) {
                let server = server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    count = SocketConnectionManager_1.SocketConnectionManager.instance.getClubOnlineCount(clubid);
                }
                else {
                    count = 0;
                    // let response = await SocketClientReqManager.instance.req_server_onlie_club(clubid, server)
                    // if (response && response["count"]) {
                    //     count = response["count"]
                    // }
                }
                onlineNum += count;
            }
            return onlineNum;
        });
    }
    getClubOnlineNum(clubid) {
        return __awaiter(this, void 0, void 0, function* () {
            let server_list = ServerConfig_1.ServerConfig.hall_server_List;
            let onlineNum = yield this.getServerListClubOnlineNum(server_list, clubid);
            Logger_1.Logger.log("getClubOnlineNum===", clubid, onlineNum);
            return onlineNum;
        });
    }
    notify_delete_club_user(clubid, userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    // HallServer_SocketMsgHandler.notify_delete_club_user(clubid, userid)
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_delete_club_user(clubid, userid, server);
                }
            }
        });
    }
    notify_set_club_quanxian(clubid, userid, quanxian) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    // HallServer_SocketMsgHandler.notify_set_club_quanxian(clubid, userid, quanxian)
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_set_club_quanxian(clubid, userid, quanxian, server);
                }
            }
        });
    }
    notify_club_freeze(clubid, userid, user_state) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    // HallServer_SocketMsgHandler.notify_club_freeze(clubid, userid, user_state)
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_club_freeze(clubid, userid, user_state, server);
                }
            }
        });
    }
    // public async notify_set_club_admin(clubid: number, userid: number, actor: number, quanxian: string) {
    //     let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
    //     for (let i = 0; i < hall_server_list.length; i++) {
    //         let server: ServerConfig = hall_server_list[i]
    //         if (server.id == ServerConfig.nowServerConfig.id) {
    //             HallServer_SocketMsgHandler.notify_set_club_admin(clubid, userid, actor, quanxian)
    //         } else {
    //             SocketClientReqManager.instance.req_server_notify_set_club_admin(clubid, userid, actor, quanxian, server)
    //         }
    //     }
    // }
    insertRoomTemplateModel() {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    RoomTemplateModel_1.RoomTemplateModel.init();
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_insertRoomTemplateModel(server);
                }
            }
        });
    }
    deleteRoomTemplate(id) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    RoomTemplateModel_1.RoomTemplateModel.deleteRoomTemplate(id);
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_club_delete_room_template(id, server);
                }
            }
        });
    }
    createRoom(room) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    RoomModel_1.RoomModel.roomMap.set(room.roomId, room);
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_createRoom(room, server);
                }
            }
            let serverConfig = ServerConfig_1.ServerConfig.getGameServerById(room.serverId);
            if (serverConfig) {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_internal_createRoom(room, serverConfig);
            }
            else {
                Logger_1.Logger.warn("找不到服务器id", room);
            }
        });
    }
    joinRoom(room, player) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                    // RoomModel.roomMap.set(room.room_id, room)
                }
                else {
                    SocketClientReqManager_1.SocketClientReqManager.instance.req_server_joinRoom(room.room_id, player, server);
                }
            }
            let serverConfig = ServerConfig_1.ServerConfig.getGameServerById(room.server_id);
            if (serverConfig) {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_joinRoom(room.room_id, player, serverConfig);
            }
            else {
                Logger_1.Logger.warn("找不到服务器id", room);
            }
        });
    }
    leaveRoom(room) {
        return __awaiter(this, void 0, void 0, function* () {
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_leave_room(room, server);
            }
        });
    }
    deleteRoom(room_id) {
        return __awaiter(this, void 0, void 0, function* () {
            RoomModel_1.RoomModel.roomMap.delete(room_id);
            let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
            for (let i = 0; i < hall_server_list.length; i++) {
                let server = hall_server_list[i];
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_deleteRoom(room_id, server);
            }
        });
    }
    notify_game_start(room) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_game_start(room, server);
        }
    }
    notify_update_gems(userid) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                HallServer_SocketMsgHandler_1.HallServer_SocketMsgHandler.notify_update_gems(userid);
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_gems(userid, server);
            }
        }
    }
    notify_update_coins(userid) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                HallServer_SocketMsgHandler_1.HallServer_SocketMsgHandler.notify_update_coins(userid);
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_coins(userid, server);
            }
        }
        let gameServerList = ServerConfig_1.ServerConfig.getAllGameServer();
        for (let i = 0; i < gameServerList.length; i++) {
            let server = gameServerList[i];
            SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_coins(userid, server);
        }
    }
    notify_update_income(userid) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_coins(userid, server);
            }
        }
    }
    notify_update_meili(userid) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_meili(userid, server);
            }
        }
    }
    notify_update_lv(userid) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_lv(userid, server);
            }
        }
    }
    notify_update_invite(userid) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_invite(userid, server);
            }
        }
    }
    notify_update_img(userid, imgurl, upload_pic_type) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_update_img(userid, imgurl, upload_pic_type, server);
        }
    }
    notify_club_open_or_close(clubid, state) {
        let hall_server_list = ServerConfig_1.ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server = hall_server_list[i];
            if (server.id == ServerConfig_1.ServerConfig.nowServerConfig.id) {
                SocketConnectionManager_1.SocketConnectionManager.instance.notify_club_open_or_close(clubid, state);
            }
            else {
                SocketClientReqManager_1.SocketClientReqManager.instance.req_server_notify_club_open_or_close(clubid, state, server);
            }
        }
    }
    delete_room_by_hall(room_id, server, nick_name) {
        SocketClientReqManager_1.SocketClientReqManager.instance.req_server_delete_room_by_hall(room_id, nick_name, server);
    }
}
exports.ServerManager = ServerManager;
ServerManager.instance = new ServerManager();
